package cn.bdqn.utils;
import lombok.Data;

import java.util.List;

/**
 * @author 梁非凡
 * @date 2018/7/11 16:00
 */
@Data
public class PageBean<T>{
    /** 当前页 */
    private Integer currentPage;  //已知
    /** 每页显示条数 */
    private Integer pageSize;     //已知

    //2. 响应需要的数据
    /** 总条数 */
    private Integer totalCount;   //select count(1) from table

    /** 总页数 */
    private Integer totalPage;
    /*   21 / 5 = 4
    pageSize        totalCount      totalPage
    5               10              ->  2
    5               15              ->  3
    5               20              ->  4
    5               21              ->  5
    if(totalCount % pageSize == 0){
        totalCount / pageSize;
    }else{
        totalCount / pageSize + 1;
    }
    公式:
    totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
     */

    //3. mysql数据库 limit查询需要起始索引

    /** 起始索引 */
    private Integer startIndex;
    /*
    currentPage     pageSize        startIndex
    1               5               0
    2               5               5
    3               5               10
    4               5               15
    公式:
    startIndex = (currentPage - 1) * pageSize;
     */


    //具体页面要显示的数据
    /** 具体显示的数据 */
    private List list;

    /**
     *   初始化分页工具类
     * @param currentPage
     *      当前页
     * @param pageSize
     *      每页显示条数
     * @param totalCount
     *      总条数
     */
    public PageBean(
            Integer currentPage,
            Integer pageSize,
            Integer totalCount) {
        this.currentPage = currentPage!=null||currentPage<1?currentPage:1;
        this.pageSize = pageSize!=null||pageSize<1?pageSize:5;
        this.totalCount = totalCount!=null||totalCount<0?totalCount:0;
        //计算出总页数和起始索引.
        this.totalPage = this.totalCount % this.pageSize == 0 ? this.totalCount /this. pageSize : this.totalCount / this.pageSize + 1;
        this.startIndex = (this.currentPage - 1) * this.pageSize;
    }

}
